const app = require("../app");
const { NAME_OR_PWD_IS_REQUIRED, NAME_IS_ALREADY_EXISTS, NAME_IS_NOT_EXISTS, PASSWORD_IS_ERROR, UNAUTHORIZED, NO_PERMISSION } = require("../config/error");

app.on('error', (errType, ctx) => {
  let code = 0
  let msg = '未知错误'

  switch (errType) {
    case NAME_OR_PWD_IS_REQUIRED:
      code = -1001
      msg = '用户名或密码不能为空'
      break;
    case NAME_IS_ALREADY_EXISTS:
      code = -1002
      msg = '用户已经注册'
      break;
    case NAME_IS_NOT_EXISTS:
      code = -1003
      msg = '用户名不存在'
      break;
    case PASSWORD_IS_ERROR:
      code = -1004
      msg = '密码错误'
      break;
    case UNAUTHORIZED:
      code = -1005
      msg = '无效token 请检查token是否过期'
      break
    case NO_PERMISSION:
      code = -2001
      msg = '没有操作该资源的权限'
      break
    default:
      break;
  }

  ctx.body = {
    code,
    msg
  }
})